//
//  OilCardTableViewCell.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/20.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "OilCardTableViewCell.h"
#import "UIImageView+Webcache.h"

@interface OilCardTableViewCell()

@property (strong, nonatomic) UIButton *bgView1;
@property (strong, nonatomic) UIImageView *pictureImageView1;
@property (strong, nonatomic) UILabel *titleLabel1;
@property (strong, nonatomic) UILabel *priceLabel1;

@property (strong, nonatomic) UIButton *bgView2;
@property (strong, nonatomic) UIImageView *pictureImageView2;
@property (strong, nonatomic) UILabel *titleLabel2;
@property (strong, nonatomic) UILabel *priceLabel2;

@end

@implementation OilCardTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        CGFloat width = (SCREEN_WIDTH - 3 * TRANS_VALUE(5.0f)) / 2;
        CGFloat height = TRANS_VALUE(76.0f);
        
        self.bgView1 = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(5.0f), TRANS_VALUE(5.0f), width, height)];
        self.bgView1.backgroundColor = I_COLOR_WHITE;
        self.bgView1.clipsToBounds = YES;
        self.bgView1.layer.cornerRadius = TRANS_VALUE(4.0f);
        self.bgView1.layer.borderColor = I_COLOR_DIVIDER.CGColor;
        self.bgView1.layer.borderWidth = 1.0f;
        [self.contentView addSubview:self.bgView1];
        
        self.pictureImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(5.0f), TRANS_VALUE(13.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
        self.pictureImageView1.contentMode = UIViewContentModeScaleAspectFit;
        self.pictureImageView1.clipsToBounds = YES;
        self.pictureImageView1.layer.cornerRadius = TRANS_VALUE(50.0f) / 2;
        self.pictureImageView1.layer.borderColor = I_COLOR_GRAY.CGColor;
        self.pictureImageView1.layer.borderWidth = 0.5f;
        [self.bgView1 addSubview:self.pictureImageView1];
        self.pictureImageView1.image = [UIImage imageNamed:@"ic_oilcard_default1"];
        
        self.titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(60.0f), TRANS_VALUE(28.0f), TRANS_VALUE(90.0f), TRANS_VALUE(20.0f))];
        self.titleLabel1.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
        self.titleLabel1.textColor = I_COLOR_33BLACK;
        self.titleLabel1.text = @"中石化加油卡";
        [self.bgView1 addSubview:self.titleLabel1];
        
        self.priceLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(60.0f), TRANS_VALUE(40.0f), TRANS_VALUE(90.0f), TRANS_VALUE(14.0f))];
        self.priceLabel1.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
        self.priceLabel1.textColor = I_COLOR_RED;
        self.priceLabel1.text = @"￥3000";
        [self.bgView1 addSubview:self.priceLabel1];
        self.priceLabel1.hidden = YES;
        
        self.bgView2 = [[UIButton alloc] initWithFrame:CGRectMake(2 * TRANS_VALUE(5.0f) + width, TRANS_VALUE(5.0f), width, height)];
        self.bgView2.backgroundColor = I_COLOR_WHITE;
        self.bgView2.clipsToBounds = YES;
        self.bgView2.layer.cornerRadius = TRANS_VALUE(4.0f);
        self.bgView2.layer.borderColor = I_COLOR_DIVIDER.CGColor;
        self.bgView2.layer.borderWidth = 1.0f;
        [self.contentView addSubview:self.bgView2];
        
        self.pictureImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(5.0f), TRANS_VALUE(13.0f), TRANS_VALUE(50.0f), TRANS_VALUE(50.0f))];
        self.pictureImageView2.contentMode = UIViewContentModeScaleAspectFit;
        self.pictureImageView2.clipsToBounds = YES;
        self.pictureImageView2.layer.cornerRadius = TRANS_VALUE(50.0f) / 2;
        self.pictureImageView2.layer.borderColor = I_COLOR_GRAY.CGColor;
        self.pictureImageView2.layer.borderWidth = 0.5f;
        [self.bgView2 addSubview:self.pictureImageView2];
        self.pictureImageView2.image = [UIImage imageNamed:@"ic_oilcard_default2"];
        
        self.titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(60.0f), TRANS_VALUE(28.0f), TRANS_VALUE(90.0f), TRANS_VALUE(20.0f))];
        self.titleLabel2.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
        self.titleLabel2.textColor = I_COLOR_33BLACK;
        self.titleLabel2.text = @"中石化加油卡";
        [self.bgView2 addSubview:self.titleLabel2];
        
        self.priceLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(60.0f), TRANS_VALUE(40.0f), TRANS_VALUE(90.0f), TRANS_VALUE(14.0f))];
        self.priceLabel2.font = [UIFont systemFontOfSize:TRANS_VALUE(12.0f)];
        self.priceLabel2.textColor = I_COLOR_RED;
        self.priceLabel2.text = @"￥3000";
        [self.bgView2 addSubview:self.priceLabel2];
        self.priceLabel2.hidden = YES;
        
        [self.bgView1 addTarget:self action:@selector(cardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView2 addTarget:self action:@selector(cardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    self.contentView.backgroundColor = I_COLOR_WHITE;
    
    return self;
}

- (void)cardButtonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    CommodityInfo *oilCardInfo = nil;
    if(button == self.bgView1) {
        oilCardInfo = _oilCardItem1;
    } else {
        oilCardInfo = _oilCardItem2;
    }
    if([_delegate respondsToSelector:@selector(clickOilCardForItem:)]) {
        [_delegate clickOilCardForItem:oilCardInfo];
    }
}

- (void)setOilCardItem1:(CommodityInfo *)cardItem {
    _oilCardItem1 = cardItem;
    if(_oilCardItem1) {
        self.bgView1.hidden = NO;
        NSString *titleStr = _oilCardItem1.productName != nil ? _oilCardItem1.productName : @"";
        self.titleLabel1.text = titleStr;
        if(_oilCardItem1.imageUrl) {
            NSString *imageURL = [NSString stringWithFormat:@"%@", _oilCardItem1.imageUrl];
            [self.pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"ic_oilcard_default1"]];
        }
        NSString *priceStr = _oilCardItem1.price;
        self.priceLabel1.text = [NSString stringWithFormat:@"￥%@", priceStr];
    } else {
        self.bgView1.hidden = YES;
    }
}

- (void)setOilCardItem2:(CommodityInfo *)cardItem {
    _oilCardItem2 = cardItem;
    if(_oilCardItem2) {
        self.bgView2.hidden = NO;
        NSString *titleStr = _oilCardItem2.productName != nil ? _oilCardItem2.productName : @"";
        self.titleLabel2.text = titleStr;
        if(_oilCardItem2.imageUrl) {
            NSString *imageURL = [NSString stringWithFormat:@"%@", _oilCardItem2.imageUrl];
            [self.pictureImageView2 sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"ic_oilcard_default2"]];
        }
        NSString *priceStr = _oilCardItem2.price;
        self.priceLabel2.text = [NSString stringWithFormat:@"￥%@", priceStr];
    } else {
        self.bgView2.hidden = YES;
    }
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
